using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SharpCore.Data;
using SharpCore.Utilities;

namespace Dating.DA
{
	public class SuccessStoryDA
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors
		public SuccessStoryDA()
		{
		}

		public SuccessStoryDA(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the SuccessStory table.
		/// </summary>
		public  int Insert(SuccessStory successStory)
		{
			ValidationUtility.ValidateArgument("successStory", successStory);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", successStory.TenantID),
				new SqlParameter("@ProfileID", successStory.ProfileID),
				new SqlParameter("@PhoneNumber", successStory.PhoneNumber),
				new SqlParameter("@EmailID", successStory.EmailID),
				new SqlParameter("@StoryText", successStory.StoryText)
			};

			successStory.SuccessStoryID = Convert.ToInt32(SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "SuccessStoryInsert", parameters));
			return successStory.SuccessStoryID;
		}

		/// <summary>
		/// Updates a record in the SuccessStory table.
		/// </summary>
		public  int Update(SuccessStory successStory)
		{
			ValidationUtility.ValidateArgument("successStory", successStory);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@SuccessStoryID", successStory.SuccessStoryID),
				new SqlParameter("@TenantID", successStory.TenantID),
				new SqlParameter("@ProfileID", successStory.ProfileID),
				new SqlParameter("@PhoneNumber", successStory.PhoneNumber),
				new SqlParameter("@EmailID", successStory.EmailID),
				new SqlParameter("@StoryText", successStory.StoryText)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "SuccessStoryUpdate", parameters));
		}

		/// <summary>
		/// Deletes a record from the SuccessStory table by its primary key.
		/// </summary>
		public  int Delete(int successStoryID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@SuccessStoryID", successStoryID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "SuccessStoryDelete", parameters));
		}

		/// <summary>
		/// Deletes a record from the SuccessStory table by a foreign key.
		/// </summary>
		public  int DeleteAllByProfileID(int profileID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ProfileID", profileID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "SuccessStoryDeleteAllByProfileID", parameters));
		}

		/// <summary>
		/// Deletes a record from the SuccessStory table by a foreign key.
		/// </summary>
		public  int DeleteAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "SuccessStoryDeleteAllByTenantID", parameters));
		}

		/// <summary>
		/// Selects a single record from the SuccessStory table.
		/// </summary>
		public  SuccessStory Select(int successStoryID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@SuccessStoryID", successStoryID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "SuccessStorySelect", parameters))
			{
				if (dataReader.Read())
				{
					return MakeSuccessStory(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the SuccessStory table.
		/// </summary>
		public  List<SuccessStory> SelectAll(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
			};
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "SuccessStorySelectAll", parameters))
			{
				List<SuccessStory> successStoryList = new List<SuccessStory>();
				while (dataReader.Read())
				{
					SuccessStory successStory = MakeSuccessStory(dataReader);
					successStoryList.Add(successStory);
				}

				return successStoryList;
			}
		}

		/// <summary>
		/// Selects all records from the SuccessStory table by a foreign key.
		/// </summary>
		public  List<SuccessStory> SelectAllByProfileID(int profileID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ProfileID", profileID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "SuccessStorySelectAllByProfileID", parameters))
			{
				List<SuccessStory> successStoryList = new List<SuccessStory>();
				while (dataReader.Read())
				{
					SuccessStory successStory = MakeSuccessStory(dataReader);
					successStoryList.Add(successStory);
				}

				return successStoryList;
			}
		}

		/// <summary>
		/// Selects all records from the SuccessStory table by a foreign key.
		/// </summary>
		public  List<SuccessStory> SelectAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "SuccessStorySelectAllByTenantID", parameters))
			{
				List<SuccessStory> successStoryList = new List<SuccessStory>();
				while (dataReader.Read())
				{
					SuccessStory successStory = MakeSuccessStory(dataReader);
					successStoryList.Add(successStory);
				}

				return successStoryList;
			}
		}

		/// <summary>
		/// Creates a new instance of the SuccessStory class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected  SuccessStory MakeSuccessStory(SqlDataReader dataReader)
		{
			SuccessStory successStory = new SuccessStory();
			successStory.SuccessStoryID = SqlClientUtility.GetInt32(dataReader, "SuccessStoryID", 0);
			successStory.TenantID = SqlClientUtility.GetInt32(dataReader, "TenantID", 0);
			successStory.ProfileID = SqlClientUtility.GetInt32(dataReader, "ProfileID", 0);
			successStory.PhoneNumber = SqlClientUtility.GetString(dataReader, "PhoneNumber", String.Empty);
			successStory.EmailID = SqlClientUtility.GetString(dataReader, "EmailID", String.Empty);
			successStory.StoryText = SqlClientUtility.GetString(dataReader, "StoryText", String.Empty);

			return successStory;
		}

		#endregion
	}
}
